"use strict";
cc._RF.push(module, 'c8cc2IitoJBTb4uc8CWiD5w', 'ziManager');
// scripts/engines/managers/ziManager.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ziDiMap = exports.ziBiMap = void 0;
var ziBi_json_1 = require("../configs/ziBi_json");
var zi_json_1 = require("../configs/zi_json");
var zi_jsonTwo_1 = require("../configs/zi_jsonTwo");
var superManager_1 = require("./superManager");
var ziBiMap = /** @class */ (function () {
    function ziBiMap() {
    }
    return ziBiMap;
}());
exports.ziBiMap = ziBiMap;
var ziDiMap = /** @class */ (function () {
    function ziDiMap() {
    }
    return ziDiMap;
}());
exports.ziDiMap = ziDiMap;
var ziManager = /** @class */ (function (_super) {
    __extends(ziManager, _super);
    function ziManager() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        //
        _this.className = '字管理';
        //字表映射
        _this.m_ziBiList = [];
        _this.m_ziBiMap = new ziBiMap();
        //字表 按笔画的2维数组
        _this.m_ziDiList = [];
        _this.m_ziDiHuaList = [];
        _this.m_ziDiHuaMap = [];
        _this.m_ziDiMap = new ziDiMap();
        //字表 按笔画的2维数组 俩笔模式
        _this.m_ziDiListOfWT = [];
        _this.m_ziDiHuaListOfWT = [];
        _this.m_ziDiHuaMapOfWT = [];
        _this.m_ziDiMapOfWT = new ziDiMap();
        return _this;
    }
    ziManager.prototype.loadManager = function () {
        var _this = this;
        return new Promise(function (resolve, reject) {
            _super.prototype.loadManager.call(_this);
            //字
            _this.m_ziBiList = ziBi_json_1.ziBi_json();
            for (var _i = 0, _a = _this.m_ziBiList; _i < _a.length; _i++) {
                var zb = _a[_i];
                _this.m_ziBiMap[zb.id] = zb;
            }
            //字笔划
            _this.m_ziDiList = zi_json_1.ziDi_json();
            for (var _b = 0, _c = _this.m_ziDiList; _b < _c.length; _b++) {
                var zd = _c[_b];
                var biCount = zd.biCount;
                //
                if (null == _this.m_ziDiHuaList[biCount]) {
                    _this.m_ziDiHuaList[biCount] = [];
                }
                _this.m_ziDiHuaList[biCount].push(zd);
                //笔划MAP
                if (null == _this.m_ziDiHuaMap[biCount]) {
                    _this.m_ziDiHuaMap[biCount] = new ziDiMap();
                }
                _this.m_ziDiHuaMap[biCount][zd.id] = zd;
                //
                _this.m_ziDiMap[zd.id] = zd;
            }
            _this.m_ziDiListOfWT = zi_jsonTwo_1.ziDi_jsonWT();
            for (var _d = 0, _e = _this.m_ziDiListOfWT; _d < _e.length; _d++) {
                var zd = _e[_d];
                var biCount = zd.biCount;
                //
                if (null == _this.m_ziDiHuaListOfWT[biCount]) {
                    _this.m_ziDiHuaListOfWT[biCount] = [];
                }
                _this.m_ziDiHuaListOfWT[biCount].push(zd);
                //笔划MAP
                if (null == _this.m_ziDiHuaMapOfWT[biCount]) {
                    _this.m_ziDiHuaMapOfWT[biCount] = new ziDiMap();
                }
                _this.m_ziDiHuaMapOfWT[biCount][zd.id] = zd;
                //
                _this.m_ziDiMapOfWT[zd.id] = zd;
            }
            resolve();
        });
    };
    /**
     * 获取字信息
     * @param id
     */
    ziManager.prototype.getZiDiInfo = function (id) {
        return this.m_ziDiMap[id];
    };
    ziManager.prototype.getZiDiInfoOfWT = function (id) {
        return this.m_ziDiMapOfWT[id];
    };
    /**
     * 获取指定笔画数所有字
     * @param biCount
     */
    ziManager.prototype.getZiDiMapOfBiCount = function (biCount) {
        return this.m_ziDiHuaMap[biCount];
    };
    ziManager.prototype.getZiDiMapOfBiCountOfWT = function (biCount) {
        return this.m_ziDiHuaMapOfWT[biCount];
    };
    /**
     * 获取笔划信息
     * @param id
     * */
    ziManager.prototype.getZiBiInfo = function (id) {
        return this.m_ziBiMap[id];
    };
    ziManager.prototype.getMakeZi = function (zId, bId, makeP) {
        var zInfo = this.getZiDiInfo(zId);
        //
        var descBiCount = zInfo.biCount + 1;
        //获取对应笔画的字
        var ziMap = this.getZiDiMapOfBiCount(descBiCount);
        //
        var isHaveBf = false;
        //
        for (var ziId in ziMap) {
            var zi = ziMap[ziId];
            //
            if (!zi.makeList)
                continue;
            for (var _i = 0, _a = zi.makeList; _i < _a.length; _i++) {
                var make = _a[_i];
                //单字 和 字笔划对应
                if (make.z == zId && make.b == bId) {
                    isHaveBf = true;
                    //并且位置合法
                    for (var _b = 0, _c = make.zbPoss; _b < _c.length; _b++) {
                        var pos = _c[_b];
                        if (makeP.start == pos.start && makeP.end == pos.end) {
                            return ziId;
                        }
                    }
                }
            }
        }
        if (isHaveBf) {
            return 'have';
        }
        return null;
    };
    ziManager.prototype.getMakeZiEx = function (zId, bId, makeP) {
        var zInfo = this.getZiDiInfoOfWT(zId);
        //
        var descBiCount = zInfo.biCount + 2;
        //获取对应笔画的字
        var ziMap = this.getZiDiMapOfBiCountOfWT(descBiCount);
        //
        var isHaveBf = false;
        //
        for (var ziId in ziMap) {
            var zi = ziMap[ziId];
            //
            if (!zi.makeList)
                continue;
            for (var _i = 0, _a = zi.makeList; _i < _a.length; _i++) {
                var make = _a[_i];
                //
                var ziBiArray = make.b.split(" ", 2);
                if (!(make.z == zId))
                    continue;
                var eachOfWord = this.splitZbPosArray(zId, ziBiArray, make.zbPoss);
                //单字 和 字笔划对应
                if (make.z == zId) {
                    isHaveBf = true;
                    //并且位置合法
                    var location = this.isLocation(makeP, eachOfWord, bId);
                    if (location) {
                        return ziId;
                    }
                }
            }
        }
        if (isHaveBf) {
            return 'have';
        }
        return null;
    };
    /**
    * 判断笔划对应位置是否合法
    * @param finger
    * @param afterSplit
    * @param wStructArray
    * @return ziMake[]
    */
    ziManager.prototype.isLocation = function (finger, afterSplit, wStructArray) {
        var isLocaLeagl = false;
        var isFristLegl = false; //第一笔合法
        for (var _i = 0, wStructArray_1 = wStructArray; _i < wStructArray_1.length; _i++) {
            var iWStruct = wStructArray_1[_i];
            var _loop_1 = function (iZiMake) {
                if (iZiMake.b == iWStruct.id) {
                    for (var _i = 0, _a = iZiMake.zbPoss; _i < _a.length; _i++) {
                        var pos = _a[_i];
                        //第一笔
                        if (!isFristLegl && finger[0].start == pos.start && finger[0].end == pos.end) {
                            isFristLegl = true;
                            break;
                        }
                        if (isFristLegl && finger[1].start == pos.start && finger[1].end == pos.end) {
                            isLocaLeagl = true;
                            break;
                        }
                    }
                    afterSplit = afterSplit.filter(function (item) { return item != iZiMake; });
                    return "break";
                }
            };
            for (var _a = 0, afterSplit_1 = afterSplit; _a < afterSplit_1.length; _a++) {
                var iZiMake = afterSplit_1[_a];
                var state_1 = _loop_1(iZiMake);
                if (state_1 === "break")
                    break;
            }
        }
        return isLocaLeagl;
    };
    /**
     * 根据start：0 end：0划分数组 每一笔划对应一个位置数组
     * @param zId
     * @param wordTwo(俩笔)
     * @param posArrOfMPos
     * @return ziMake[]
     */
    ziManager.prototype.splitZbPosArray = function (zId, wordTwo, posArrOfMPos) {
        //
        var ziMakeArray = [];
        var isSentinel = false; //哨兵start:0 end:0 
        // let isSentinels: number = 0;//哨兵start:0 end:0        
        var mPosFrist = [];
        var mPosSecond = [];
        var array = {};
        for (var _i = 0, posArrOfMPos_1 = posArrOfMPos; _i < posArrOfMPos_1.length; _i++) {
            var pos = posArrOfMPos_1[_i];
            if (!(pos.start == 0 && pos.end == 0)) {
                if (isSentinel) {
                    mPosSecond.push(pos);
                    // array[isSentinels].zbPoss.push(pos);
                    continue;
                }
                mPosFrist.push(pos);
            }
            if (pos.start == 0 && pos.end == 0) {
                isSentinel = true;
                // isSentinels++;
                continue;
            }
        }
        for (var index = 0; index < wordTwo.length; index++) {
            var ziMakeMessage = void 0;
            if (index == 0) {
                ziMakeMessage = {
                    z: zId,
                    b: wordTwo[index],
                    zbPoss: mPosFrist,
                };
            }
            if (index == 1) {
                ziMakeMessage = {
                    z: zId,
                    b: wordTwo[index],
                    zbPoss: mPosSecond,
                };
            }
            ziMakeArray.push(ziMakeMessage);
        }
        return ziMakeArray;
    };
    /**
     * 获取坐标对应的田字坐标
     * @param vc2
     */
    ziManager.prototype.getMakePosOfVec2 = function (vc2, size) {
        var oneSize = size.width / 3;
        var oneHref = oneSize / 2;
        //
        var x = 1;
        if (vc2.x > -oneHref && vc2.x < oneHref)
            x = 2;
        else if (vc2.x > oneHref)
            x = 3;
        //
        var y = 0;
        if (vc2.y < oneHref && vc2.y > -oneHref)
            y = 1;
        else if (vc2.y < -oneHref)
            y = 2;
        //
        var xy = y * 3 + x;
        return xy;
    };
    /**
     * 获取能组成的所有字
     * @param srcZiId 单字
     */
    ziManager.prototype.getCanMakeDoZiIds = function (srcZiId) {
        var srcZiInfo = this.getZiDiInfo(srcZiId);
        var allCountZis = this.getZiDiMapOfBiCount(srcZiInfo.biCount + 1);
        var ziIdList = [];
        for (var id in allCountZis) {
            var zi = allCountZis[id];
            //
            if (!zi.makeList)
                continue;
            for (var _i = 0, _a = zi.makeList; _i < _a.length; _i++) {
                var make = _a[_i];
                if (make.z == srcZiId) {
                    ziIdList.push(zi.id);
                }
            }
        }
        return ziIdList;
    };
    /**
     * 获取能组成的所有字 俩笔
     * @param srcZiId 单字
     */
    ziManager.prototype.getCanMakeDoZiIdsEx = function (srcZiId) {
        var srcZiInfo = this.getZiDiInfoOfWT(srcZiId);
        var allCountZis = this.getZiDiMapOfBiCountOfWT(srcZiInfo.biCount + 2);
        var ziIdList = [];
        for (var id in allCountZis) {
            var zi = allCountZis[id];
            //
            if (!zi.makeList)
                continue;
            for (var _i = 0, _a = zi.makeList; _i < _a.length; _i++) {
                var make = _a[_i];
                if (make.z == srcZiId) {
                    ziIdList.push(zi.id);
                }
            }
        }
        return ziIdList;
    };
    /**
     * 查找能组成的字
     * @param biIds 有序笔划
     * */
    ziManager.prototype.findCanMakZiIds = function (biIds) {
        var biCount = biIds.length;
        var srcZiArray = this.getZiDiMapOfBiCount(biCount);
        var ziOks = [];
        //
        for (var id in srcZiArray) {
            var zi = srcZiArray[id];
            if (this.isCalcSuccessZiIdOfBiIds(zi, biIds)) {
                ziOks.push(zi.id);
            }
        }
        //
        return ziOks;
    };
    ziManager.prototype.findCanMakZiIdsOfWT = function (biIds) {
        var biCount = biIds.length;
        var srcZiArray = this.getZiDiMapOfBiCountOfWT(biCount);
        var ziOks = [];
        //
        for (var id in srcZiArray) {
            var zi = srcZiArray[id];
            if (this.isCalcSuccessZiIdOfBiIds(zi, biIds)) {
                ziOks.push(zi.id);
            }
        }
        //
        return ziOks;
    };
    /**
     * 是否匹配
     * @param ziId 字ID
     * @param biIds 笔划有序ID
     */
    ziManager.prototype.isCalcSuccessZiIdOfBiIds = function (ziInfo, biIds) {
        //
        var orderBiList = ziInfo.orderBiList;
        //
        if (!orderBiList)
            return false;
        //
        if (biIds.length != orderBiList.length) {
            return false;
        }
        //
        var needRightCount = orderBiList.length;
        //容错次数
        var canWrongCount = 0;
        if (needRightCount >= 20) {
            canWrongCount = 2;
        }
        else if (needRightCount >= 10) {
            canWrongCount = 1;
        }
        else {
            canWrongCount = 0;
        }
        for (var i = 0; i < biIds.length; i++) {
            if (biIds[i] == orderBiList[i])
                continue;
            //模糊判断
            if (orderBiList[i] == '丶' && biIds[i] == '㇏') {
            }
            else {
                canWrongCount--;
            }
            //错次数太多
            if (canWrongCount < 0) {
                return false;
            }
        }
        return true;
    };
    return ziManager;
}(superManager_1.default));
exports.default = ziManager;

cc._RF.pop();